//
//  HMPhotoSelectorController.swift
//  照片选择器
//
//  Created by iOS on 16/10/6.
//  Copyright © 2016年 iOS. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class HMPhotoSelectorController: UICollectionViewController {
    private let maxOfItem = 6
    
    /// 用户选中的图片
    var photos = [UIImage]()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private let layout = UICollectionViewFlowLayout()
    
    init() {
        super.init(collectionViewLayout: layout)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupCollectionView()
    }
    
    private func setupCollectionView() {
        self.collectionView!.register(HMPhotoSelectorCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        layout.itemSize = CGSize(width: 80, height: 80)
        layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // 当图片没有到最大张数时,图片数量+1
        // 当图片等于最大张数时,最大张数
        return photos.count < maxOfItem ? photos.count + 1 : maxOfItem
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! HMPhotoSelectorCell
        
        cell.delegate = self
        if indexPath.item < photos.count {
            // 有图片,可以取到
            let image = photos[indexPath.item]
            cell.image = image
        } else {
            // 最后的那个加号按钮,设置cell显示加号按钮
            cell.setupAddButton()
        }
    
        return cell
    }
}

extension HMPhotoSelectorController: HMPhotoSelectorCellDelegate {
    func photoSelectorCellDidClickDeleteButton(cell: HMPhotoSelectorCell) {
        // 根据cell找到对应的indexPath.item
        if let indexPath = collectionView?.indexPath(for: cell) {
            photos.remove(at: indexPath.item)
            
            collectionView?.reloadData()
        }
    }
    
    func photoSelectorCellDidClickAddButton() {
        // 弹出系统的照片选择器
        let picker = UIImagePickerController()
        picker.delegate = self
        
        self.present(picker, animated: true, completion: nil)
    }
}

extension HMPhotoSelectorController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    // 实现这个方法后,控制器就不会自动关闭,需要我们手动关闭
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        print("info: \(info)")
        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
            let newImage = image.scaleImage()
            print("newImage: \(newImage)")
            photos.append(newImage)
            
            // 刷新数据
            collectionView?.reloadData()
        }
        
        picker.dismiss(animated: true, completion: nil)
    }
}
